package com.sky.service.impl;

import com.sky.Entity.OrderDetail;
import com.sky.Entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrdersStatisticsVO;
import com.sky.vo.OrdersTop10VO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserStatisticsVo;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderDetailMapper detailMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        //计算日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        Map map=new HashMap<>();
        map.put("status", Orders.COMPLETED);
        List<Double> turnoverList=new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime startTime=LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(localDate,LocalTime.MAX);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            Double turnover=orderMapper.queryTurnoverByMap(map);
            if (turnover == null) {
                turnover=0.0d;
            }
            turnoverList.add(turnover);
        }

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    @Override
    public UserStatisticsVo userStatistics(LocalDate begin, LocalDate end) {
        //计算日期
        List<LocalDate> dateList=new ArrayList<>();
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> totalUserList=new ArrayList<>();
        List<Integer> newUserList=new ArrayList<>();
        for (LocalDate localDate : dateList) {

            LocalDateTime startTime=LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(localDate,LocalTime.MAX);
            Map map=new HashMap();
            map.put("endTime",endTime);
            Integer totalUsers=userMapper.queryByMap(map);
            map.put("startTime",startTime);
            Integer newUsers=userMapper.queryByMap(map);

            if (totalUsers == null) {
                totalUsers=0;
            }
            if (newUsers == null) {
                newUsers=0;
            }
            totalUserList.add(totalUsers);
            newUserList.add(newUsers);
        }


        return UserStatisticsVo.builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .build();
    }

    @Override
    public OrdersStatisticsVO ordersStatistics(LocalDate begin, LocalDate end) {
        //计算日期
        List<LocalDate> dateList=new ArrayList<>();
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> totalList=new ArrayList<>();
        List<Integer> validList=new ArrayList<>();
        for (LocalDate localDate : dateList) {

            LocalDateTime startTime=LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(localDate,LocalTime.MAX);
            Map map=new HashMap();
            map.put("endTime",endTime);
            map.put("startTime",startTime);
            Integer totalCount=orderMapper.queryOrderCountByMap(map);
            map.put("status",Orders.COMPLETED);
            Integer validCount=orderMapper.queryOrderCountByMap(map);

            if (totalCount == null) {
                totalCount=0;
            }

            if (validCount == null) {
                validCount=0;
            }

            totalList.add(totalCount);
            validList.add(validCount);
        }
        Integer totalCount=totalList.stream().reduce(Integer::sum).get();
        Integer validCount=validList.stream().reduce(Integer::sum).get();
        Double orderCompletionRate=0d;//完成率
        if(totalCount!=0){
            orderCompletionRate=validCount.doubleValue()/totalCount;
        }


        return OrdersStatisticsVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCompletionRate(orderCompletionRate)
                .totalOrderCount(totalCount)
                .validOrderCount(validCount)
                .orderCountList(StringUtils.join(totalList,","))
                .validOrderCountList(StringUtils.join(validList,","))
                .build();
    }

    @Override
    public OrdersTop10VO top10(LocalDate begin, LocalDate end) {

        LocalDateTime startTime=LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime=LocalDateTime.of(end,LocalTime.MAX);

        Map map=new HashMap();
        map.put("endTime",endTime);
        map.put("startTime",startTime);
        List<OrderDetail> list=detailMapper.queryByMap(map);

        if (list == null) {
            list=new ArrayList<>();
        }

        List<String> nameList = list.stream().map(OrderDetail::getName).collect(Collectors.toList());
        List<Integer> numberList = list.stream().map(OrderDetail::getNumber).collect(Collectors.toList());


        return OrdersTop10VO.builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();
    }


}
